package com.vm.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.vm.activity.AboutAcitivity;
import com.vm.activity.DetailActivity;
import com.vm.activity.SettingActivity;
import com.vm.activity.ShareActivity;
import com.vm.activity.SoftUninstallActivity;
import com.vm.activity.SoftUpgradeActivity;
import com.vm.db.UpgradeSoftDao;
import com.vm.model.UpgradeSoft;
import com.wwt.app.R;

public class ManagerFragment extends BaseFragment{

	RelativeLayout rlayout_upgrade;
	Button btn_software_upgrade_number;
	Button btn_software_uninstall;
	Button btn_installation_package_management;
	// Button btn_download_management;
	Button btn_clean_up;
	Button btn_share;
	Button btn_skin;
	Button btn_feedback;
	Button btn_set_up;
	Button btn_exit;
	Button btn_accelerate;
	Button btn_about;
	
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("ManagerFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.manage1, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initButton();
		
		UpgradeSoftDao dao = new UpgradeSoftDao(getActivity());

		List<UpgradeSoft> appList = dao.getUpgradeSofts();
		// 好啦 这下手机上安装的应用数据都存在appList里了。
		btn_software_upgrade_number.setText(Integer.toString(appList.size()));
	}

	/**
	 * 初始化按钮
	 */
	public void initButton() {

		rlayout_upgrade = (RelativeLayout) view.findViewById(R.id.rlayout_upgrade);
		btn_software_upgrade_number = (Button) view.findViewById(R.id.btn_software_upgrade_number);
		btn_software_uninstall = (Button) view.findViewById(R.id.btn_software_uninstall);
		btn_installation_package_management = (Button) view.findViewById(R.id.btn_installation_package_management);
		// btn_download_management =
		// (Button)findViewById(R.id.btn_download_management);
		btn_clean_up = (Button) view.findViewById(R.id.btn_clean_up);
		btn_accelerate = (Button) view.findViewById(R.id.btn_accelerate);
		btn_share = (Button) view.findViewById(R.id.btn_share);
		btn_skin = (Button) view.findViewById(R.id.btn_skin);
		btn_feedback = (Button) view.findViewById(R.id.btn_feedback);
		btn_set_up = (Button) view.findViewById(R.id.btn_set_up);
		btn_about = (Button) view.findViewById(R.id.btn_about);
//		btn_exit = (Button) view.findViewById(R.id.btn_exit);
		

		rlayout_upgrade.setOnClickListener(btnClickListener);
		btn_software_uninstall.setOnClickListener(btnClickListener);
		btn_installation_package_management
				.setOnClickListener(btnClickListener);
		// btn_download_management.setOnClickListener(btnClickListener);
		btn_clean_up.setOnClickListener(btnClickListener);
		btn_accelerate.setOnClickListener(btnClickListener);
		btn_share.setOnClickListener(btnClickListener);
		btn_skin.setOnClickListener(btnClickListener);
		btn_feedback.setOnClickListener(btnClickListener);
		btn_set_up.setOnClickListener(btnClickListener);
		btn_about.setOnClickListener(btnClickListener);
//		btn_exit.setOnClickListener(btnClickListener);

	}

	/**
	 * 设置监听器
	 */
	public OnClickListener btnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent;
			switch (v.getId()) {
			case R.id.rlayout_upgrade:
				intent = new Intent(getActivity(),
						SoftUpgradeActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_software_uninstall:
				intent = new Intent(getActivity(),
						SoftUninstallActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_installation_package_management:

				break;
			case R.id.btn_clean_up:
				startAnotherApp("com.tencent.qqpimsecure");
				break;
			case R.id.btn_accelerate:
				startAnotherApp("com.tencent.qqpimsecure");
				break;
			case R.id.btn_share:
				intent = new Intent(getActivity(),
						ShareActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_skin:

				break;
			case R.id.btn_feedback:

				break;
			case R.id.btn_about:
				intent = new Intent(getActivity(), AboutAcitivity.class);
				startActivity(intent);
				break;

			case R.id.btn_set_up:
				intent = new Intent(getActivity(), SettingActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_exit:

				break;

			default:
				break;
			}

		}
	};

	private void startAnotherApp(String packageName) {
//		PackageInfo packageInfo = null;
		try {
			Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);  
			startActivity(LaunchIntent);  
//			packageInfo = getPackageManager().getPackageInfo(packageName, 0);
//			if (packageInfo == null) {
//				
//			} else {
//				System.out.println("packageInfo!=null");
//				Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
//				resolveIntent.setData(Uri
//						.parse("app://jp.co.cybird.barcodefootballer/"));
//				resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//				resolveIntent.setPackage(packageInfo.packageName);
//				System.out
//						.println("packageInfo.packageName=" + packageInfo.packageName);
//
//				List<ResolveInfo> resolveInfoList = getPackageManager()
//						.queryIntentActivities(resolveIntent, 0);
//
//				System.out.println("resolveInfoList.size()=" + resolveInfoList.size());
//
//				ResolveInfo resolveInfo = resolveInfoList.iterator().next();
//				if (resolveInfo != null) {
//					String activityPackageName = resolveInfo.activityInfo.packageName;
//					String className = resolveInfo.activityInfo.name;
//
//					Intent intent = new Intent(Intent.ACTION_MAIN);
//					intent.addCategory(Intent.CATEGORY_LAUNCHER);
//					ComponentName componentName = new ComponentName(
//							activityPackageName, className);
//
//					intent.setComponent(componentName);
//					startActivity(intent);
//				}
//				
//			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("packageInfo==null");
			Intent intent = new Intent(getActivity(),DetailActivity.class);
			intent.putExtra("softId", "579d884a-4011-4d5f-a6ed-f4f2580533bb");
			startActivity(intent);
		}

		// <data android:scheme="app"
		// android:host="jp.co.cybird.barcodefootballer/" />

		

	}
	
	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		
	}

}
